import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

const routes = [{
        path: '/',
        name: 'Login',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../components/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('../components/Register.vue')
    },
    {
        path: '/adminHome',
        name: 'AdminHome',
        component: () =>
            import ('../components/admin/AdminHome'),
        children: [{
                path: '/setClass',
                name: 'SetClass',
                component: () =>
                    import ('../components/admin/SetClass'),
            },
            {
                path: '/userInfo',
                name: 'UserInfo',
                component: () =>
                    import ('../components/admin/UserInfo'),
            },
            {
                path: '/infoStatistics',
                name: 'InfoStatistics',
                component: () =>
                    import ('../components/admin/InfoStatistics'),
            },
            {
                path: '/chart',
                name: 'Chart',
                component: () =>
                    import ('../components/admin/Chart'),
            },
        ]
    },
    {
        path: '/studentHome',
        name: 'StudentHome',
        component: () =>
            import ('../components/student/StudentHome')
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由拦截
router.beforeEach(function(to, from, next) {
    if (to.path == '/' || to.path == null || to.path == '/register') {
        next()
    } else {
        let level = sessionStorage.getItem('level')
        if ((level === null || level === '') || (level == '学生' && (to.path != '/StudentHome'))) {
            next({
                path: '/'
            })
        } else {
            next();
        }
    }
})

export default router